# 商家分析页面
<template>
  <div class="merchant-analysis">
    <el-card class="merchant-info-card">
      <template #header>
        <div class="card-header">
          <span class="shop-name">{{ statistics.shopName }}</span>
        </div>
      </template>
      
      <el-row :gutter="20" class="statistics-cards">
        <el-col :span="6">
          <el-card shadow="hover" class="stat-card">
            <div class="stat-content">
              <div class="stat-icon">
                <el-icon><ShoppingCart /></el-icon>
              </div>
              <div class="stat-info">
                <div class="stat-label">总订单数</div>
                <div class="stat-value">{{ statistics.totalOrders || 0 }}</div>
              </div>
            </div>
          </el-card>
        </el-col>
        <el-col :span="6">
          <el-card shadow="hover" class="stat-card">
            <div class="stat-content">
              <div class="stat-icon">
                <el-icon><ShoppingBag /></el-icon>
              </div>
              <div class="stat-info">
                <div class="stat-label">总销量</div>
                <div class="stat-value">{{ statistics.totalSales || 0 }}</div>
              </div>
            </div>
          </el-card>
        </el-col>
        <el-col :span="6">
          <el-card shadow="hover" class="stat-card">
            <div class="stat-content">
              <div class="stat-icon">
                <el-icon><Money /></el-icon>
              </div>
              <div class="stat-info">
                <div class="stat-label">总收入</div>
                <div class="stat-value">¥{{ formatNumber(statistics.totalRevenue || 0) }}</div>
              </div>
            </div>
          </el-card>
        </el-col>
        <el-col :span="6">
          <el-card shadow="hover" class="stat-card">
            <div class="stat-content">
              <div class="stat-icon">
                <el-icon><DataLine /></el-icon>
              </div>
              <div class="stat-info">
                <div class="stat-label">商家ID</div>
                <div class="stat-value">{{ statistics.merchantId || '-' }}</div>
              </div>
            </div>
          </el-card>
        </el-col>
      </el-row>
    </el-card>
  </div>
</template>

<script setup>
import { ref, computed, onMounted, watch } from 'vue'
import { useRoute } from 'vue-router'
import { ElMessage } from 'element-plus'
import {
  ShoppingCart,
  ShoppingBag,
  Money,
  DataLine
} from '@element-plus/icons-vue'
import { getMerchantSalesStatistics } from '@/api/statistics'

const route = useRoute()
const merchantId = computed(() => route.params.id)

// 状态定义
const loading = ref(false)
const statistics = ref({
  shopName: '',
  totalOrders: 0,
  totalSales: 0,
  totalRevenue: 0,
  merchantId: null
})

// 获取商家销售统计
const fetchStatistics = async () => {
  if (!merchantId.value) return
  
  try {
    loading.value = true
    const res = await getMerchantSalesStatistics(merchantId.value)
    if (res.code === 200 && res.data) {
      statistics.value = res.data
    }
  } catch (error) {
    console.error('获取商家销售统计失败:', error)
    ElMessage.error('获取商家销售统计失败')
  } finally {
    loading.value = false
  }
}

// 格式化数字
const formatNumber = (num) => {
  return Number(num).toLocaleString('zh-CN', {
    minimumFractionDigits: 2,
    maximumFractionDigits: 2
  })
}

// 监听商家ID变化
watch(
  () => route.params.id,
  (newId) => {
    if (newId) {
      fetchStatistics()
    }
  },
  { immediate: true }
)

onMounted(() => {
  if (merchantId.value) {
    fetchStatistics()
  }
})
</script>

<style scoped>
.merchant-analysis {
  padding: 20px;
}

.merchant-info-card {
  margin-bottom: 20px;
}

.shop-name {
  font-size: 18px;
  font-weight: bold;
  color: var(--el-text-color-primary);
}

.statistics-cards {
  margin-top: 20px;
}

.stat-card {
  height: 120px;
  transition: all 0.3s;
}

.stat-card:hover {
  transform: translateY(-5px);
  box-shadow: 0 2px 12px 0 rgba(0,0,0,0.1);
}

.stat-content {
  display: flex;
  align-items: center;
  height: 100%;
  padding: 0 20px;
}

.stat-icon {
  font-size: 48px;
  color: var(--el-color-primary);
  margin-right: 20px;
}

.stat-info {
  flex: 1;
}

.stat-label {
  font-size: 14px;
  color: var(--el-text-color-secondary);
  margin-bottom: 8px;
}

.stat-value {
  font-size: 24px;
  font-weight: bold;
  color: var(--el-text-color-primary);
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
</style>
